<!--
 * @Description: 
 * @version: 
 * @Author: mikeJang
 * @Date: 2021-08-20 09:07:05
 * @LastEditors: mikeJang
 * @LastEditTime: 2021-08-21 10:56:54
 * @FilePath: \YU_LIN_Frontend\src\components\SDeptSelect.vue
-->
<template>
  <a-select
    size="large"
    show-search
    :filter-option="filterOption"
    v-model="deptId"
    @change="handelChange"
    placeholder="请选择"
    :disabled="disabled"
  >
    <a-select-option
      v-for="item in superiorData"
      :value="item.id"
      :key="item.id"
    >
      {{ item.deptName }}
    </a-select-option>
  </a-select>
</template>

<script>
export default {
  name: "SDeptSelect",
  props: {
    adcd: {
      type: String
    },
    value: {
      type: String
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      superiorData: [],
      deptId: this.value,
      filter: this.adcd
    };
  },
  mounted() {},
  watch: {
    adcd() {
      this.filter = this.adcd || null;
      this.getSuperiorDataByAdcd();
    },
    deptId(val) {
      if (val) {
        this.$emit("change-done", val);
      }
    }
  },
  created() {
    if (this.filter) {
      this.getSuperiorDataByAdcd(this.value);
    }
  },
  methods: {
    // 根据行政区划获取机构
    getSuperiorDataByAdcd(flag) {
      const params = {
        adcd: this.filter
      }
      this.$http.userauth.queryDeptByAdcd(params).then(res => {
          this.superiorData = res ? res : [];
          this.deptId = flag ? this.value : res ? res[0].id : "";
          this.$emit("input", this.deptId);
        });
    },
    handelChange(val) {
      // this.$emit("change", val);
      this.$emit("input", val);
    },
    filterOption(input, option) {
      return (
        option.componentOptions.children[0].text
          .toLowerCase()
          .indexOf(input.toLowerCase()) >= 0
      );
    }
  }
};
</script>
